#!/bin/sh

### BEGIN INIT INFO
# Provides:          batman
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     4 5
# Default-Stop:      0 1 6
# Short-Description: Battery management
# Description:       Battery management program for Linux phones
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=batman
DAEMON=/usr/bin/$NAME
DESC="Battery management program for Linux phones"
LOG_FILE="/var/log/batman.log"
ERROR_LOG_FILE="/var/log/batman.err.log"
CONFIG_FILE=""

if [ -f /etc/batman/config ]; then
   CONFIG_FILE="/etc/batman/config"
elif [ -f /var/lib/batman/config ]; then
   CONFIG_FILE="/var/lib/batman/config"
fi

[ -n "${CONFIG_FILE}" ] && . "${CONFIG_FILE}"

[ -x "$DAEMON" ] || exit 0

. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon --start --quiet --background \
        --exec $DAEMON -- ${BATMAN_ARGS} \
        >> "${LOG_FILE}" 2>> "${ERROR_LOG_FILE}"
    log_end_msg $?
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --oknodo --exec $DAEMON --retry 30
    log_end_msg $?
    ;;
  force-reload|reload)
    log_daemon_msg "Reloading $DESC" "$NAME"
    log_end_msg 3
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    status_of_proc $DAEMON $NAME
    exit $?
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status|force-reload|reload}"
    exit 1
    ;;
esac

exit 0
